//
//  ViewController.m
//  BKLoading
//
//  Created by TOPTEAM on 2016/11/23.
//  Copyright © 2016年 TOPTEAM. All rights reserved.
//

#import "ViewController.h"
#import "shadowView.h"
@interface ViewController ()<CAAnimationDelegate>

@property (nonatomic,strong)UIImageView *imgV;

@property (nonatomic,strong)NSMutableArray * marr_anim;
@property (nonatomic,strong)NSMutableArray * marr_anim_imgV;
@property (nonatomic,strong)NSMutableArray * marr_anim_shadow;

@property (nonatomic,strong)shadowView   * view_shadow;

@property (weak, nonatomic) IBOutlet UIButton *open;
- (IBAction)OPEN:(UIButton *)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor=[UIColor  whiteColor];
    _marr_anim = [NSMutableArray array];
    _marr_anim_imgV = [NSMutableArray array];
    _marr_anim_shadow = [NSMutableArray array];
    [self.view addSubview:self.view_shadow];
    [self.view addSubview:self.imgV];
    [self BKLoading];
}
-(shadowView *)view_shadow{
    if (!_view_shadow) {
        _view_shadow=[[shadowView alloc]initShadow];
        _view_shadow.Color=[UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1.0];
        _view_shadow.center = CGPointMake(200,213);
        _view_shadow.bounds = CGRectMake(0,0,20,7);
    }
    return _view_shadow;
}

-(UIImageView *)imgV{
    if (!_imgV) {
        _imgV=[[UIImageView alloc]init];
        _imgV.image=[UIImage imageNamed:@"loading"];
        _imgV.frame=CGRectMake(200, 200, 20, 20);
    }
    return _imgV;
}
-(void)BKLoading{
 
#pragma mark----第一组动画
    //小球上弹
    /*
     * 动作：
           1.小球上升
           2.阴影缩小
     */
    CAKeyframeAnimation *animation_rise = [self rise_Anima:@"position" duration:0.5f fromValue:@(200) toValue:@(150)];
    [_imgV.layer addAnimation:animation_rise forKey:nil];
    
    CAKeyframeAnimation *animation_transform_1 = [self transform_Anima:@"transform.scale" duration:0.5f fromValue:@(1.00) toValue:@(0.30)];
    [_view_shadow.layer addAnimation:animation_transform_1 forKey:nil];
    
    //    //动画组
    //    CAAnimationGroup *animGroup_1 = [CAAnimationGroup animation];
    //    animGroup_1.animations = [NSArray arrayWithObjects:animation_rise, animation_transform_1, nil];
    //    animGroup_1.duration = 0.5f;
    //    animGroup_1.delegate = self;
    //    [_marr_anim addObject:animGroup_1];
    
#pragma mark----第二组动画
    //小球旋转
    CAKeyframeAnimation *animation_spin = [self spin_Anima:@"position" duration:0.5f ];
    [_marr_anim_imgV addObject:animation_spin];
    
    CAKeyframeAnimation *animation_transform_2 = [self transform_Anima:@"transform.scale" duration:1.5f fromValue:@(0.30) toValue:@(0.29)];
    [_marr_anim_shadow addObject:animation_transform_2];
    
    //    //动画组
    //    CAAnimationGroup *animGroup_2 = [CAAnimationGroup animation];
    //    animGroup_2.animations = [NSArray arrayWithObjects:animation_spin, animation_transform_2, nil];
    //    animGroup_2.duration = 0.5f*2;
    //    animGroup_2.delegate=self;
    //
    //    [_marr_anim addObject:animGroup_2];
    
    
#pragma mark----第三组动画
    //小球下降
    CAKeyframeAnimation *animation_down = [self down_Anima:@"position" duration:0.50f fromValue:@(150) toValue:@(200)];
    [_marr_anim_imgV addObject:animation_down];
    
    CAKeyframeAnimation *animation_transform_3 = [self transform_Anima:@"transform.scale" duration:0.50f fromValue:@(0.29) toValue:@(1.00)];
    [_marr_anim_shadow addObject:animation_transform_3];
    
    
    //    //动画组
    //    CAAnimationGroup *animGroup_3 = [CAAnimationGroup animation];
    //    animGroup_3.animations = [NSArray arrayWithObjects:animation_down, animation_transform_3, nil];
    //    animGroup_3.duration = 0.5f;
    //    animGroup_3.delegate=self;
    //    [_marr_anim addObject:animGroup_3];
    
#pragma mark----第四组动画
    //小球上弹
    CAKeyframeAnimation *animation_rise_2 = [self rise_Anima:@"position" duration:0.45f fromValue:@(200) toValue:@(170)];
    [_marr_anim_imgV addObject:animation_rise_2];
    
    CAKeyframeAnimation *animation_transform_4 = [self transform_Anima:@"transform.scale" duration:0.45f fromValue:@(1.00) toValue:@(0.50)];
    [_marr_anim_shadow addObject:animation_transform_4];
    
    //    //动画组
    //    CAAnimationGroup *animGroup_4 = [CAAnimationGroup animation];
    //    animGroup_4.animations = [NSArray arrayWithObjects:animation_rise_2, animation_transform_4, nil];
    //    animGroup_4.duration = 0.5f;
    //    animGroup_4.delegate=self;
    //    [_marr_anim addObject:animGroup_4];
    
#pragma mark----第五组动画
    //小球下降
    CAKeyframeAnimation *animation_down_2 = [self down_Anima:@"position" duration:0.4f fromValue:@(170) toValue:@(200)];
    [_marr_anim_imgV addObject:animation_down_2];
    
    CAKeyframeAnimation *animation_transform_5 = [self transform_Anima:@"transform.scale" duration:0.4f fromValue:@(0.50) toValue:@(1.00)];
    [_marr_anim_shadow addObject:animation_transform_5];
    
    //    //动画组
    //    CAAnimationGroup *animGroup_5 = [CAAnimationGroup animation];
    //    animGroup_5.animations = [NSArray arrayWithObjects:animation_down_2, animation_transform_5, nil];
    //    animGroup_5.duration = 0.5f;
    //    animGroup_5.delegate=self;
    //    [_marr_anim addObject:animGroup_5];
    
#pragma mark----第六组动画
    //小球上弹
    CAKeyframeAnimation *animation_rise_3 = [self rise_Anima:@"position" duration:0.35f fromValue:@(200) toValue:@(185)];
    [_marr_anim_imgV addObject:animation_rise_3];
    
    CAKeyframeAnimation *animation_transform_6 = [self transform_Anima:@"transform.scale" duration:0.35f fromValue:@(1.00) toValue:@(0.70)];
    [_marr_anim_shadow addObject:animation_transform_6];
    
    //    //动画组
    //    CAAnimationGroup *animGroup_6 = [CAAnimationGroup animation];
    //    animGroup_6.animations = [NSArray arrayWithObjects:animation_rise_3, animation_transform_6, nil];
    //    animGroup_6.duration = 0.5f;
    //    animGroup_6.delegate=self;
    //    [_marr_anim addObject:animGroup_6];
    
#pragma mark----第七组动画
    //小球下降
    CAKeyframeAnimation *animation_down_3 = [self down_Anima:@"position" duration:0.3f fromValue:@(185) toValue:@(200)];
    [_marr_anim_imgV addObject:animation_down_3];
    
    CAKeyframeAnimation *animation_transform_7 = [self transform_Anima:@"transform.scale" duration:0.3f fromValue:@(0.70) toValue:@(1.00)];
    [_marr_anim_shadow addObject:animation_transform_7];
    
    //    //动画组
    //    CAAnimationGroup *animGroup_7 = [CAAnimationGroup animation];
    //    animGroup_7.animations = [NSArray arrayWithObjects:animation_down_3, animation_transform_7, nil];
    //    animGroup_7.duration = 0.5f;
    //    animGroup_7.delegate=self;
    //    [_marr_anim addObject:animGroup_7];
    
    
#pragma mark----第八组动画
    //小球上弹
    CAKeyframeAnimation *animation_rise_4 = [self rise_Anima:@"position" duration:0.2f fromValue:@(200) toValue:@(192)];
    [_marr_anim_imgV addObject:animation_rise_4];
    
    CAKeyframeAnimation *animation_transform_8 = [self transform_Anima:@"transform.scale" duration:0.2f fromValue:@(1.00) toValue:@(0.70)];
    [_marr_anim_shadow addObject:animation_transform_8];
    
    
#pragma mark----第九组动画
    //小球下降
    CAKeyframeAnimation *animation_down_4 = [self down_Anima:@"position" duration:0.15f fromValue:@(192) toValue:@(200)];
    [_marr_anim_imgV addObject:animation_down_4];
    
    CAKeyframeAnimation *animation_transform_9 = [self transform_Anima:@"transform.scale" duration:0.15f fromValue:@(0.70) toValue:@(1.00)];
    [_marr_anim_shadow addObject:animation_transform_9];
    
    
    
}

-(void)oopen{
    [self BKLoading];
}

- (void) animationDidStart:(CAAnimation *)anim
{
    NSLog(@"animation start");
}

- (void) animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSLog(@"animation stop");
    
    
    if (_marr_anim_shadow.count == 0) {
        return;
    }
    
    [_imgV.layer addAnimation:[_marr_anim_imgV objectAtIndex:0] forKey:nil];
    [_view_shadow.layer addAnimation:[_marr_anim_shadow objectAtIndex:0] forKey:nil];
    [_marr_anim_imgV    removeObjectAtIndex:0];
    [_marr_anim_shadow  removeObjectAtIndex:0];
    
}


/*
 * 小球上升
 */
- (CAKeyframeAnimation *)rise_Anima:(NSString *)keypath
                           duration:(CFTimeInterval)duration
                          fromValue:(id)fromValue
                            toValue:(id)toValue{
    
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = keypath;
    //    animation.repeatCount=MAXFLOAT;
    animation.repeatCount=1;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.duration = duration;
    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //    animation.delegate=self;
    animation.values =
    [self rise_basicAnimationValues:fromValue toValue:toValue duration:duration];
    return animation;
}

-(NSMutableArray *)rise_basicAnimationValues:(id)fromValue toValue:(id)toValue duration:(CGFloat)duration {
    
    NSInteger numOfFrames = duration * 60;
    NSMutableArray *values = [NSMutableArray arrayWithCapacity:numOfFrames];
    for (NSInteger i = 0; i < numOfFrames; i++) {
        [values addObject:[NSValue valueWithCGPoint:CGPointMake(200, 200)]];
    }
    CGFloat diff = [toValue floatValue] - [fromValue floatValue];
    for (NSInteger frame = 0; frame < numOfFrames; frame++) {
        CGFloat x = (CGFloat)frame / (CGFloat)numOfFrames;
        CGFloat value = [fromValue floatValue] + diff * x;
        NSValue *value_point=[NSValue valueWithCGPoint:CGPointMake(200, value)];
        values[frame] = value_point;
    }
    return values;
    
}
/*
 * 小球旋转
 */
- (CAKeyframeAnimation *)spin_Anima:(NSString *)keypath
                           duration:(CFTimeInterval)duration{
    
    CAKeyframeAnimation *orbit = [CAKeyframeAnimation animation];
    orbit.keyPath = keypath;
    orbit.path = CFAutorelease(CGPathCreateWithEllipseInRect(CGRectMake(0, 0, 1, 1), NULL));
    orbit.duration = duration;
    orbit.additive = YES;
    //    orbit.delegate=self;
    //    orbit.repeatCount = HUGE_VALF;
    orbit.repeatCount = 3;
    orbit.calculationMode = kCAAnimationPaced;
    orbit.rotationMode = kCAAnimationRotateAuto;
    return orbit;
}
/*
 * 小球下降
 */
- (CAKeyframeAnimation *)down_Anima:(NSString *)keypath
                           duration:(CFTimeInterval)duration
                          fromValue:(id)fromValue
                            toValue:(id)toValue{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = keypath;
    //    animation.repeatCount=MAXFLOAT;
    animation.repeatCount=1;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.duration = duration;
    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //    animation.delegate=self;
    animation.values =
    [self down_basicAnimationValues:fromValue toValue:toValue duration:duration];
    return animation;
}

-(NSMutableArray *)down_basicAnimationValues:(id)fromValue toValue:(id)toValue duration:(CGFloat)duration {
    
    NSInteger numOfFrames = duration * 60;
    NSMutableArray *values = [NSMutableArray arrayWithCapacity:numOfFrames];
    for (NSInteger i = 0; i < numOfFrames; i++) {
        [values addObject:[NSValue valueWithCGPoint:CGPointMake(200, 200)]];
    }
    CGFloat diff = [toValue floatValue] - [fromValue floatValue];
    for (NSInteger frame = 0; frame < numOfFrames; frame++) {
        CGFloat x = (CGFloat)frame / (CGFloat)numOfFrames;
        CGFloat value = [fromValue floatValue] + diff * x;
        NSValue *value_point=[NSValue valueWithCGPoint:CGPointMake(200, value)];
        values[frame] = value_point;
    }
    return values;
    
    
}

/*
 * 关键帧缩放
 */
- (CAKeyframeAnimation *)transform_Anima:(NSString *)keypath
                                duration:(CFTimeInterval)duration
                               fromValue:(id)fromValue
                                 toValue:(id)toValue{
    
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = keypath;
    //    animation.repeatCount=MAXFLOAT;
    animation.repeatCount=1;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.duration = duration;
    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.delegate=self;
    animation.values =
    [self transform_basicAnimationValues:fromValue toValue:toValue duration:duration];
    return animation;
    
}

-(NSMutableArray *)transform_basicAnimationValues:(id)fromValue toValue:(id)toValue duration:(CGFloat)duration {
    
    NSInteger numOfFrames = duration * 60;
    NSMutableArray *values = [NSMutableArray arrayWithCapacity:numOfFrames];
    for (NSInteger i = 0; i < numOfFrames; i++) {
        [values addObject:@(0.0)];
    }
    CGFloat diff = [toValue floatValue] - [fromValue floatValue];
    for (NSInteger frame = 0; frame < numOfFrames; frame++) {
        
        CGFloat x = (CGFloat)frame / (CGFloat)numOfFrames;
        CGFloat value = [fromValue floatValue] + diff * x;
        values[frame] = @(value);
    }
    return values;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)OPEN:(UIButton *)sender {
    
     [self BKLoading];
    
}
@end























